package cn.edu.swu.wwy.auth;

import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpFilter;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;

@WebServlet(urlPatterns = "/admin/*")
public class AuthFilter extends HttpFilter {
    public final static String LOGIN_TOKEN_KEY = "LOGIN_TOKEN_KEY";

    @Override
    public void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpSession session = request.getSession(true);
        if (session.getAttribute(LOGIN_TOKEN_KEY) == null) {
            response.sendRedirect("../login.html");
        } else {
            chain.doFilter(request, response);
        }
    }
}
